<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

      <el-form-item label="模板名" prop="name">
        <el-input v-model="dataForm.name" placeholder="模板名"></el-input>
        <el-checkbox v-model="defaultName">默认填入上传的文件名</el-checkbox>
      </el-form-item>
      <el-form-item label="模板文件" prop="formwork">
        <!-- <el-input v-model="dataForm.formwork" placeholder="模板文件"></el-input> -->
        <el-upload class="upload-demo" :action="
          cdn + '/sys/uploadFiles/upload2'
        " :headers="{ token: $cookie.get('token') }" :on-success="uploadSuccess" :before-upload="beforeUpload">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="截止时间" prop="endTime1">
        <el-date-picker v-model="dataForm.endTime1" align="right" type="date" placeholder="选择日期"
          :picker-options="pickerOptions" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否开启" prop="statu">
        <el-radio v-model="dataForm.statu" :label="1">是</el-radio>
        <el-radio v-model="dataForm.statu" :label="2">否</el-radio>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      defaultName: true,
      dataForm: {
        id: 0,
        name: "",
        formwork: "",
        endTime1: "",
        statu: "",
      },
      //上传路径
      cdn: window.SITE_CONFIG['baseUrl'],
      dataRule: {
        name: [{ required: true, message: "模板名不能为空", trigger: "blur" }],
        formwork: [
          { required: true, message: "模板内容不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "截止时间不能为空", trigger: "blur" },
        ],
        statu: [
          { required: true, message: "是否开启不能为空", trigger: "blur" },
        ],
      },

      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      value1: "",
      value2: "",
    };
  },
  methods: {
    beforeUpload(file) {
      console.log('上传文件', file);
      if (this.defaultName)
        this.dataForm.name = file.name
    },
    uploadSuccess(res) {
      console.log(res);
      this.dataForm.formwork = res.fileName;
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/formwork/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.formwork.name;
              this.dataForm.formwork = data.formwork.formwork;
              this.dataForm.endTime1 = data.formwork.endTime1;
              this.dataForm.statu = data.formwork.statu;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/formwork/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              formwork: this.dataForm.formwork,
              endTime1: this.dataForm.endTime1,
              // endTime: this.dataForm.endTime1,
              statu: this.dataForm.statu,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },

    // 上传
    handleRemove(file, fileList) {
      this.fileName = file.name;
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
  },
};
</script>
